package com.nbjtjc.safe.api.sys;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.sys.SysUserMapper;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.service.sys.SysUserReportService;

@RestController
@CrossOrigin("*")
@RequestMapping("/sysUserReport")
public class SysUserReportController {
	@Autowired
	SysUserReportService sysUserReportService;
	@Autowired
	SysUserMapper sysUserMapper;
	
	  @RequestMapping(value = "/addOrUpdate",method = {RequestMethod.POST})
	  @SysLog("添加或更新人员花名册")
	    public Result add(@RequestBody List<SysUser> sysUsers,@RequestBody String year){
	        Result result=new Result();
	        try {
	            result=sysUserReportService.addByYear(sysUsers,year);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/addOrUpdateThisYear",method = {RequestMethod.POST})
	  @SysLog("添加或更新本年人员花名册")
	    public Result addOrUpdateThisYear(){
	        Result result=new Result();
	        try {
	        	List<SysUser> sysUsers = new ArrayList();
	        	Date date = new Date();	        	
	        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
	        	
	        	sysUsers = sysUserMapper.search(new SysUser(), new Page());
	        	
	            result=sysUserReportService.addByYear(sysUsers,sdf.format(date));
	            
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/searchByYear",method = {RequestMethod.POST})
	    public Result searchByYear(@RequestBody String year){
	        Result result=new Result();
	        try {
	            result=sysUserReportService.searchByYear(year);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }

}
